import java.util.Arrays;
import java.util.Comparator;
import java.util.Objects;

class Book implements Comparable{
    private int id;//书号
    private String name;//书名
    private double price;//价格
    private String author;//作者
    private String press;//出版社
    private String time;//出版时间

    public Book(int id, String name, double price, String author, String press, String time) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.author = author;
        this.press = press;
        this.time = time;
    }

    public Book() {
    }

    @Override
    public boolean equals(Object o) {//重写equals方法
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Book book = (Book) o;
        return id == book.id && Double.compare(book.price, price) == 0 && Objects.equals(name, book.name) && Objects.equals(author, book.author) && Objects.equals(press, book.press) && Objects.equals(time, book.time);
    }

    @Override
    public int hashCode() {//重写哈希
        return Objects.hash(id, name, price, author, press, time);
    }

    @Override
    public String toString() {//重写toString方法
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", price=" + price +
                ", author='" + author + '\'' +
                ", press='" + press + '\'' +
                ", time='" + time + '\'' +
                '}';
    }

    @Override
    public int compareTo(Object o){//实现自然排序,按书名
        Book book=(Book) o;
        return this.getName().compareTo(((Book) o).getName());
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getPress() {
        return press;
    }

    public void setPress(String press) {
        this.press = press;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }
}


public class Test {
    public static void main(String[] args) {
        Book[] books=new Book[]{new Book(01,"java设计",50.0,"张三","中国人民出版社","2022-02-02"),new Book(01,"C语言设计",50.2,"李四","中国人民出版社","2022-01-01")};
       //System.out.println(books[0].equals(books[1]));
       // System.out.println(books[0].hashCode());
        Arrays.sort(books);//按书名排序
        for (int i = 0; i < books.length; i++) {
            System.out.println(books[i]);
        }
        Arrays.sort(books, new Comparator<Book>() {
            @Override
            public int compare(Book o1, Book o2) {//按作者排序
                return o1.getAuthor().compareTo(o2.getAuthor());
            }
        });
        Arrays.sort(books, new Comparator<Book>() {
            @Override
            public int compare(Book o1, Book o2) {//按出版时间排序
                return o1.getTime().compareTo(o2.getTime());
            }
        });
        Arrays.sort(books, new Comparator<Book>() {
            @Override
            public int compare(Book o1, Book o2) {//按出版社排序
                return o1.getPress().compareTo(o2.getPress());
            }
        });Arrays.sort(books, new Comparator<Book>() {
            @Override
            public int compare(Book o1, Book o2) {//按价格排序
                return ((Double)o1.getPrice()).compareTo(o2.getPrice());
            }
        });
    }

}
